# (C) Datadog, Inc. 2023-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from __future__ import annotations

import sys
from typing import TYPE_CHECKING

import click

if TYPE_CHECKING:
    from ddev.cli.application import Application


@click.command(short_help='Serve documentation')
@click.option(
    '--dirty',
    is_flag=True,
    help='Speed up reload time by only rebuilding edited pages (based on modified time). For development only.',
)
@click.pass_obj
def serve(app: Application, dirty):
    """
    Serve documentation.
    """
    command = [sys.executable, '-m', 'hatch', 'run', 'docs:serve']
    if dirty:
        app.display_warning(
            'Using dirty reload - NOTE: When editing autogenerated content (such as docstrings for API documentation), '
            're-save the `.md` file that contains the output to trigger a refresh.'
        )
        command.append('--dirtyreload')

    with app.repo.path.as_cwd(env_vars={'HATCH_VERBOSITY': str(app.verbosity)}):
        app.platform.exit_with_command(command)
